// 引入第三方包 express
var express = require('express')

// 实例化express
var app = express()

// 设置静态资源托管文件夹

app.use(express.static('static'))

// 需要在所有接口前设置处理post请求参数的中间件
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

// 定义返回给客户端的数据
var arr = [
    { email: '178884773@qq.com', name: '张麻子', phone: '13611129070', path: '1234', ingroup: '编辑组', isadmin: 'true', status: true },
    { email: '178884773@qq.com', name: '汤师爷', phone: '13611129070', path: '1234', ingroup: '编辑组', isadmin: 'true', status: true },
    { email: '178884773@qq.com', name: '黄四郎', phone: '13611129070', path: '1234', ingroup: '编辑组', isadmin: 'true', status: false },
    { email: '2697294080@qq.com', name: '张三', phone: '15592085265', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
    { email: '2697294080@qq.com', name: '李四', phone: '15592085265', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
    { email: '2697294080@qq.com', name: '王麻子', phone: '15592085265', pass: '1234', ingroup: '编辑组', isadmin: true, status: false },
    { email: '2697294080@qq.com', name: '老君山', phone: '15592085265', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
    { email: '2697294080@qq.com', name: '华山', phone: '15592085265', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
    { email: '2697294080@qq.com', name: '玉龙雪山', phone: '15592085265', pass: '1234', ingroup: '编辑组', isadmin: true, status: false },
    { email: '2697294080@qq.com', name: '黄山', phone: '15592085265', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
    { email: '2697294080@qq.com', name: '太白山', phone: '15592085265', pass: '1234', ingroup: '编辑组', isadmin: true, status: true },
    { email: '2697294080@qq.com', name: '泰山', phone: '15592085265', pass: '1234', ingroup: '编辑组', isadmin: true, status: false }
]

// 设置分页(查询)接口
app.get('/user/list', (req, res) => {

    // 获取get类型请求的参数：req.query
    var { page, count } = req.query;

    console.log('收到了来自客户端的请求');

    console.log(page, count);

    // req 请求对象，包含了和本次请求相关的所有信息(请求地址，请求方式，携带的参数)

    // res 响应对象，可以借助res向客户端返回信息(字符串，json格式数据，网页)


    // 分页

    var newarr = arr.slice((page - 1) * count, page * count);
    // 返回数据
    res.send({ code: 200, msg: '请求成功', list: newarr })


})

// 登录请求
app.post('/user/login', (req, res) => {

    var { phone, path } = req.body;
    console.log(phone, path);

    // 用来自客户端的数据 做判断，判断是否在服务器端的用户列表中
    var i = arr.findIndex((item, index) => {
        return item.phone == phone && item.path == path;
    })

    // 如果i不是-1，说明来自客户端的数据是合法的
    if (i != -1) {
        // 登陆成功时，可以一并返回 登陆成功的用户信息，登录令牌
        res.send({ code: 200, msg: '登录成功！', userinfo: arr[i], token: '' })
    } else {
        res.send({ code: 400, msg: '登陆失败！' })
    }



})
// 模糊搜索
app.get('/user/search', (req, res) => {
    // 接收参数
    var { searchkey } = req.query;

    // 根据参数，从用户的name属性中进行过滤
    var newarr = arr.filter((item) => {
        //includes() 返回true/false
        return item.name.includes(searchkey);
    })

    // 返回数据
    res.send({ code: 200, msg: '查询成功！', list: newarr })
})





// 设置监听
app.listen(8080, () => {
    console.log('服务端运行在: http://localhost:8080');
})